<script>
//实现sum(1,2)(2)() == 5
//    sum(3)(3)()   == 6

//方法1
function sum(...args){
    sum.s = sum.s || 0
    if(args.length == 0){
        let a = sum.s
        sum.s = 0
        return a
    }else{
        args.forEach(item => {
            sum.s += item
        })
        return sum
    }
}

let a = sum(1,2)(2)() //5
console.log(a)
let b = sum(3)(3)()   //6
console.log(b)
let c = sum()         //0
console.log(c)

//方法2
function sum2(...args){
    let s = args.reduce((pre, next) => pre + next)
    return function(...args2){
        if(args2.length == 0){
            return s
        }else{
            let s2 = args2.reduce((pre, next) => pre + next)
            return sum2(s + s2)
        }
    }
}

let a1 = sum2(1,2)(2)() //5
console.log(a1)
let b1 = sum2(3)(3)()   //6
console.log(b1)

</script>